package com.cwt_taxi_xa.db.dal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cwt_taxi_xa.model.MessageCenter;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;

public class MessageDao {
	private ContentValues values;
	private DBOpenHelper helper;
	private static final String TAG = "MessageDao";

	public MessageDao(Context context){
		values = new ContentValues();
		helper = new DBOpenHelper(context);
	}

	/**
	 * 新增消息中心信息
	 * @author ferch
	 * @param center
	 */
	public synchronized long insertMessageCenter(MessageCenter center) {
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			values.clear();
			ContentValues values = new ContentValues();
			values.put(DBConstants.MESSAGE_CONTENT, center.getContent());
			values.put(DBConstants.MESSAGE_CREATE_TIME, center.getCreatetime());
			values.put(DBConstants.MESSAGE_IS_READ, center.getIsread());
			rowId = db.insert(DBConstants.TABLE_MESSAGE, null, values);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
		return rowId;
	}

	/**
	 * 更新是否阅读的调度信息（0未读，1已读）
	 * @author ferch
	 * @param orderid
	 */
	public synchronized void clearMessage(){
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			int result = -1;
			result = db.delete(DBConstants.TABLE_MESSAGE, null, null);
			//		db.execSQL("delete from sqlite_sequence where name = \"" + Constants.TABLENAME+ "\"");
			db.execSQL("update sqlite_sequence set seq = 0 where name = \""+ DBConstants.TABLE_MESSAGE+ "\"");
			AppLog.i(TAG, "清空表数据: tableName message; result:"+result);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
	}

	/**
	 * 清空调度信息
	 * @author ferch
	 * @param orderid
	 */
	public synchronized void updateMessageCenter(int id){
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			String sql = "update "+DBConstants.TABLE_MESSAGE+" set isread = 1 where "+DBConstants.MESSAGE_ID+" = '"+id+"'";
			db.execSQL(sql);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
	}

	/**
	 * 获取未读调度信息总条数
	 * @author ferch
	 * @return
	 */
	public synchronized int getNotReadMSGCount(){
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			values.clear();
			String sql = "select count(*) from "+DBConstants.TABLE_MESSAGE+" where "+DBConstants.MESSAGE_IS_READ+"=0";
			Cursor c = db.rawQuery(sql, null);
			c.moveToFirst();
			int length = c.getInt(0);
			db.close();
			return length;
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
			// TODO: handle exception
			return 0;
		}

	}

	/**
	 * 查询消息中心信息
	 * @author ferch
	 * @param currentPage
	 * @param pageCount
	 * @return
	 */
	public synchronized List<MessageCenter> findMessageCenter(int currentPage,int pageCount){
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			String sql = "1 = 1 order by "+DBConstants.MESSAGE_CREATE_TIME+" desc limit "+((currentPage-1)*pageCount)+","+pageCount;
			Cursor cursor = db.query(DBConstants.TABLE_MESSAGE, null, sql, null,
					null, null, null);
			List<MessageCenter> list = new ArrayList<MessageCenter>();
			MessageCenter mc = null;
			if(cursor.getCount()!=0){
				while (cursor.moveToNext()) {
					mc = new MessageCenter();
					mc.setContent(cursor.getString(cursor.getColumnIndex(DBConstants.MESSAGE_CONTENT)));
					mc.setCreatetime(cursor.getString(cursor.getColumnIndex(DBConstants.MESSAGE_CREATE_TIME)));
					mc.setId(cursor.getInt(cursor.getColumnIndex(DBConstants.MESSAGE_ID)));
					mc.setIsread(cursor.getInt(cursor.getColumnIndex(DBConstants.MESSAGE_IS_READ)));
					list.add(mc);
				}
			}
			db.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
		return null;
	}

	/**
	 * 获取消息中心信息总条数
	 * @author ferch
	 * @return
	 */
	public synchronized int getMessageCenterCount(){
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			values.clear();
			String sql = "select count(*) from "+DBConstants.TABLE_MESSAGE;
			Cursor c = db.rawQuery(sql, null);
			c.moveToFirst();
			int length = c.getInt(0);
			db.close();
			return length;
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
			// TODO: handle exception
			return 0;
		}

	}

	/**
	 * 根据id查询消息中心信息
	 * @author ferch
	 * @param id
	 * @return
	 */
	public synchronized MessageCenter getMessageCenterById(String id){
		try {
			String select = "id="+id;
			SQLiteDatabase db = helper.getWritableDatabase();
			Cursor cursor = db.query("message", null, select,
					null, null, null, null);
			MessageCenter mc = new MessageCenter();
			if(cursor.getCount() !=0){
				if(cursor.moveToFirst()){
					mc.setContent(cursor.getString(cursor.getColumnIndex(DBConstants.MESSAGE_CONTENT)));
					mc.setCreatetime(cursor.getString(cursor.getColumnIndex(DBConstants.MESSAGE_CREATE_TIME)));
					mc.setId(cursor.getInt(cursor.getColumnIndex(DBConstants.MESSAGE_ID)));
					mc.setIsread(cursor.getInt(cursor.getColumnIndex(DBConstants.MESSAGE_IS_READ)));
				}
			}
			db.close();
			return mc;
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
			return null;
		}
	}

}
